﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using Styx;
using Styx.CommonBot.Frames;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace F2FTraderq
{
    public partial class Main
    {
        public Stopwatch Sw3 = new Stopwatch();
        public static string ClearSendMail = "ClearSendMail();";

        public static string Stackof20Part1 =
            "for b=0,4 do for s=0,32 do local l=GetContainerItemLink(b,s)if l and l:find(\"";

        public static string Stackof20Part2 =
            "\")and select(2,GetContainerItemInfo(b,s))==20 then UseContainerItem(b,s)end end end";

        public int ShortBreak = new Random().Next(400, 700);
        public static string MailFrameTab2Click = "MailFrameTab2:Click();";
        public static string SendMailCodButtonClick = "SendMailCODButton:Click();";
        public static string SetGold = "SendMailMoneyGold:SetText(\"";
        public static string SetSilver = "SendMailMoneySilver:SetText(\"";
        public static string SetCopper = "SendMailMoneyCopper:SetText(\"";
        public static string SendMailNameEditBox = "SendMailNameEditBox:SetText(\"";
        public static string ClosingBracket = "\");";
        public static string GetSendMailItem12 = "return GetSendMailItem(12)";
        public static uint Zero = 0;
        public static string TxtFileExtension = ".txt";
        public static string CodMessage1 = "Sending 12 stacks of ";
        public static string CodMessage2 = " to ";
        public static string CodMessage3 = " for ";
        public static string CodMessage4 = " Gold, ";
        public static string CodMessage5 = " Silver, ";
        public static string CodMessage6 = " Copper";
        public static string ClickSendMailButton = "RunMacroText(\"/click SendMailMailButton\")";
        public int LongBreak = new Random().Next(3000, 5000);
        public int MediumBreak = new Random().Next(1000, 2000);
        public string BlacklistDirectoryLocation = "Plugins" + "\\" + "F2FTrader";
        public string BlacklistFileLocation = "Plugins" + "\\" + "F2FTrader" + "\\" + "blacklist.txt";

        public void SendingMailLog(string itemToSend, string recipient, string goldamount, string silveramount,
                                   string copperamount)
        {
            Log(CodMessage1 +
                itemToSend +
                CodMessage2 +
                recipient +
                CodMessage3 +
                goldamount +
                CodMessage4 +
                silveramount +
                CodMessage5 +
                copperamount +
                CodMessage6);
        }

        public void WriteRecipientToFile(string recipient)
        {
            if (Directory.Exists(MainPath + DoubleDash + Year + DoubleDash + Month +
                                 DoubleDash + DayNumber))
            {
                using (StreamWriter writer = File.AppendText(MainPath + DoubleDash + Year + DoubleDash + Month +
                                                             DoubleDash + DayNumber +
                                                             DoubleDash + DayOfWeek + TxtFileExtension))
                {
                    Info("Added: " + recipient);
                    writer.WriteLine(recipient);
                }
            }
            else
            {
                Directory.CreateDirectory(MainPath + DoubleDash + Year + DoubleDash + Month +
                                          DoubleDash + DayNumber);
            }
        }

        public void MailSentSuccessful()
        {
        }

        public void MoneyOfferedChanged()
        {
            Lua.DoString("CancelTrade();");
            Info("Canceled Trade in Progress as money offered has changed... or Trade Window was closed");
        }

        public void TradeMoneyMatchesSetRequirement()
        {
            if (MIRsettings.Instance.Face2Face)
            {
                Log("TRADE_MONEY_CHANGED");
                try
                {
                    var tradeMoney = Lua.GetReturnVal<int>("return GetTargetTradeMoney()", 0);

                    var count = tradeMoney.ToString(CultureInfo.InvariantCulture).Count() - 4;

                    Log(tradeMoney.ToString(CultureInfo.InvariantCulture));

                    var gold = tradeMoney.ToString(CultureInfo.InvariantCulture).Remove(count);
                    var silver = tradeMoney.ToString(CultureInfo.InvariantCulture).Substring(count).Remove(2, 2);
                    var copper = tradeMoney.ToString(CultureInfo.InvariantCulture).Substring(count).Remove(0, 2);

                    Log("Unconverted Value: " + gold + " " + silver + " " + copper);

                    Log("Converted Value: " +
                        Convert.ToInt32(gold) + " " +
                        Convert.ToInt32(silver) + " " +
                        Convert.ToInt32(copper));

                    Log("MIRSettings Value: " + MIRsettings.Instance.Gold + MIRsettings.Instance.Silver +
                        MIRsettings.Instance.Copper);

                    if (Convert.ToInt32(gold) >= MIRsettings.Instance.Gold &&
                        Convert.ToInt32(silver) >= MIRsettings.Instance.Silver &&
                        Convert.ToInt32(copper) >= MIRsettings.Instance.Copper)
                    {
                        Info("Money offered by Player is correct, Accepting Trade");
                        Lua.DoString("AcceptTrade();");
                    }
                }
                catch (Exception e)
                {
                    Warning("Unable to Convert: " + e.Message + e.Source + e.Data + e.StackTrace + e.InnerException);
                }
            }

            for (var i = 0; i < 7; i++)
            {
                foreach (
                    WoWItem item in
                        StyxWoW.Me.BagItems.Where(
                            item => item.Entry == Convert.ToInt32(RemoveDigits(MIRsettings.Instance.SelectedItem))).
                            Where(item => item != null && item.StackCount == 20))
                {
                    Info("Adding Selected Item to Trade Window");
                    item.UseContainerItem();
                    i++;
                }
            }
            //GetPlayerTradeMoney
            //GetTargetTradeMoney
            /*
            var tradeMoney = Lua.GetReturnVal<int>("return GetPlayerTradeMoney()", 0);

            var count = tradeMoney.ToString(CultureInfo.InvariantCulture).Count() - 4;

            var gold = tradeMoney.ToString(CultureInfo.InvariantCulture).Remove(count);
            var silver = tradeMoney.ToString(CultureInfo.InvariantCulture).Substring(count).Remove(2, 2);
            var copper = tradeMoney.ToString(CultureInfo.InvariantCulture).Substring(count).Remove(0, 2);
            
            Log.Logging.Write("Unconverted Value: " + gold + " " + silver + " " + copper);

            Log.Logging.Write("Converted Value: " + 
                Convert.ToInt32(gold) + " " +
                Convert.ToInt32(silver) + " " +
                Convert.ToInt32(copper));

            Log.Logging.Write("MIRSettings Value: " + MIRsettings.Instance.Gold + MIRsettings.Instance.Silver + MIRsettings.Instance.Copper);

            if (Convert.ToInt32(gold) >= MIRsettings.Instance.Gold &&
                Convert.ToInt32(silver) >= MIRsettings.Instance.Silver &&
                Convert.ToInt32(copper) >= MIRsettings.Instance.Copper)
            {
                Log.Log("Money offered by Player is correct, Accepting Trade");
                Lua.DoString("AcceptTrade();");
            }*/
        }

        public void MailboxOpen()
        {
            Lua.DoString("MailFrameTab2:Click();");
        }

        public void MailboxClosed()
        {
            if (!new MailFrame().IsVisible)
            {
                InteractWithMailbox();
            }
            else
            {
                OpenAttachments();
            }
        }

        public bool CanMoveToMailbox()
        {
            try
            {
                if (StyxWoW.Me.FreeBagSlots > 100)
                {
                    return true;
                }
            }
            catch (Exception e)
            {
                Warning("Error: " + e.Message);
            }
            return false;
        }

        public void InteractWithMailbox()
        {
            try
            {
                var mailboxes = ObjectManager.GetObjectsOfType<WoWGameObject>(false, false).Where(
                    o => o != null && o.Entry == MIRsettings.Instance.MailboxId).ToList();
                foreach (
                    WoWGameObject obj in
                        mailboxes.Where(obj => obj != null).Where(obj => obj.WithinInteractRange).Where(
                            obj => !new MailFrame().IsVisible))
                {
                    obj.Interact();
                    Info("Interacting with Mailbox...");
                    StyxWoW.SleepForLagDuration();
                }
            }
            catch (Exception e)
            {
                Warning("Error: " + e.Message);
            }
        }

        public WoWPoint MoveToMailbox()
        {
            try
            {
                var mailboxes = ObjectManager.GetObjectsOfType<WoWGameObject>(false, false).Where(
                    o => o != null && o.Entry == MIRsettings.Instance.MailboxId).ToList();
                foreach (WoWGameObject obj in mailboxes.Where(obj => obj != null))
                {
                    return obj.Location;
                }
            }
            catch (Exception e)
            {
                Warning("Error: " + e.Message);
            }
            return WoWPoint.Empty;
        }

        public void OpenAttachments()
        {
            try
            {
                if (new MailFrame().IsVisible && new MailFrame().MailCount > 0 && StyxWoW.Me.FreeBagSlots != 0 && StyxWoW.Me.FreeBagSlots != 1)
                {
                    Thread.Sleep(new Random().Next(400, 700));
                    Lua.DoString("MailFrameTab1:Click();");
                    Thread.Sleep(new Random().Next(400, 700));
                    Lua.DoString("RunMacroText(\"/click PostalOpenAllButton\");");
                    Info("Opening Mail...");
                }
                else if (!new MailFrame().IsVisible)
                {
                    InteractWithMailbox();
                }
            }
            catch (Exception e)
            {
                Warning("Error: " + e.Message);
            }
        }

        public void SendMail()
        {
            var cod1StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod1ItemToSend && o.StackCount == 20);
            var cod2StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod2ItemToSend && o.StackCount == 20);
            var cod3StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod3ItemToSend && o.StackCount == 20);
            var cod4StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod4ItemToSend && o.StackCount == 20);
            var cod5StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod5ItemToSend && o.StackCount == 20);
            var cod6StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod6ItemToSend && o.StackCount == 20);
            var cod7StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod7ItemToSend && o.StackCount == 20);
            var cod8StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod8ItemToSend && o.StackCount == 20);
            var cod9StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod9ItemToSend && o.StackCount == 20);
            var cod10StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod10ItemToSend && o.StackCount == 20);
            var cod11StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod11ItemToSend && o.StackCount == 20);
            var cod12StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod12ItemToSend && o.StackCount == 20);
            var cod13StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod13ItemToSend && o.StackCount == 20);
            var cod14StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod14ItemToSend && o.StackCount == 20);
            var cod15StackAmount = ObjectManager.GetObjectsOfType<WoWItem>(false, false).Count(
                o => o.Name == MIRsettings.Instance.Cod15ItemToSend && o.StackCount == 20);

            if (!File.Exists(MainPath + DoubleDash + Year + DoubleDash + Month +
                             DoubleDash + DayNumber +
                             DoubleDash + DayOfWeek + TxtFileExtension))
            {
                new Main().Info("Directory Created: " + MainPath + DoubleDash + Year + DoubleDash + Month +
                                DoubleDash + DayNumber);
                Directory.CreateDirectory(MainPath + DoubleDash + Year + DoubleDash + Month +
                                          DoubleDash + DayNumber);
                Thread.Sleep(1000);
                new Main().Info("File Created: " + MainPath + DoubleDash + Year + DoubleDash + Month +
                                DoubleDash + DayNumber +
                                DoubleDash + DayOfWeek + TxtFileExtension);
                File.Create(MainPath + DoubleDash + Year + DoubleDash + Month +
                            DoubleDash + DayNumber +
                            DoubleDash + DayOfWeek + TxtFileExtension);
            }
            else if (MIRsettings.Instance.Cod1Enabled &&
                cod1StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod1Recipient) <= MIRsettings.Instance.TbCod1 &&
                MIRsettings.Instance.Cod1Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod1Recipient))
            {
                InitializeCod1();
            }
            else if (MIRsettings.Instance.Cod2Enabled &&
                cod2StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod2Recipient) <= MIRsettings.Instance.TbCod2 &&
                MIRsettings.Instance.Cod2Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod2Recipient))
            {
                InitializeCod2();
            }
            else if (MIRsettings.Instance.Cod3Enabled &&
                cod3StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod3Recipient) <= MIRsettings.Instance.TbCod3 &&
                MIRsettings.Instance.Cod3Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod3Recipient))
            {
                InitializeCod3();
            }
            else if (MIRsettings.Instance.Cod4Enabled &&
                cod4StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod4Recipient) <= MIRsettings.Instance.TbCod4 &&
                MIRsettings.Instance.Cod4Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod4Recipient))
            {
                InitializeCod4();
            }
            else if (MIRsettings.Instance.Cod5Enabled &&
                cod5StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod5Recipient) <= MIRsettings.Instance.TbCod5 &&
                MIRsettings.Instance.Cod5Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod5Recipient))
            {
                InitializeCod5();
            }
            else if (MIRsettings.Instance.Cod6Enabled &&
                cod6StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod6Recipient) <= MIRsettings.Instance.TbCod6 &&
            MIRsettings.Instance.Cod6Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod6Recipient))
            {
                InitializeCod6();
            }
            else if (MIRsettings.Instance.Cod7Enabled &&
                cod7StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod7Recipient) <= MIRsettings.Instance.TbCod7 &&
                MIRsettings.Instance.Cod7Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod7Recipient))
            {
                InitializeCod7();
            }
            else if (MIRsettings.Instance.Cod8Enabled &&
                cod8StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod8Recipient) <= MIRsettings.Instance.TbCod8 &&
                MIRsettings.Instance.Cod8Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod8Recipient))
            {
                InitializeCod8();
            }
            else if (MIRsettings.Instance.Cod9Enabled &&
                cod9StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod9Recipient) <= MIRsettings.Instance.TbCod9 &&
                MIRsettings.Instance.Cod9Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod9Recipient))
            {
                InitializeCod9();
            }
            else if (MIRsettings.Instance.Cod10Enabled &&
                cod10StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod10Recipient) <= MIRsettings.Instance.TbCod10 &&
                MIRsettings.Instance.Cod10Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod10Recipient))
            {
                InitializeCod10();
            }
            else if (MIRsettings.Instance.Cod11Enabled &&
                cod11StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod11Recipient) <= MIRsettings.Instance.TbCod11 &&
                MIRsettings.Instance.Cod11Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod11Recipient))
            {
                InitializeCod11();
            }
            else if (MIRsettings.Instance.Cod12Enabled &&
                cod12StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod12Recipient) <= MIRsettings.Instance.TbCod12 &&
                MIRsettings.Instance.Cod12Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod12Recipient))
            {
                InitializeCod12();
            }
            else if (MIRsettings.Instance.Cod13Enabled &&
                cod13StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod13Recipient) <= MIRsettings.Instance.TbCod13 &&
                MIRsettings.Instance.Cod13Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod13Recipient))
            {
                InitializeCod13();
            }
            else if (MIRsettings.Instance.Cod14Enabled &&
                cod14StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod14Recipient) <= MIRsettings.Instance.TbCod14 &&
                MIRsettings.Instance.Cod14Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod14Recipient))
            {
                InitializeCod14();
            }
            else if (MIRsettings.Instance.Cod15Enabled &&
                cod15StackAmount >= 12 &&
                MailPackageSendCount(MIRsettings.Instance.Cod15Recipient) <= MIRsettings.Instance.TbCod15 &&
                MIRsettings.Instance.Cod15Ready)
                //BlacklistCheck(MIRsettings.Instance.Cod15Recipient))
            {
                InitializeCod15();
            }
            else
            {
                OpenAttachments();
            }
        }

        public bool BlacklistCheck(string recipient)
        {
            var frame = new MailFrame();
            if (File.Exists(BlacklistFileLocation))
            {
                if (!IsRecipientBlacklisted(recipient))
                {
                    using (StreamWriter write = File.AppendText(BlacklistFileLocation))
                    {
                        foreach (
                            var source in frame.GetAllMails().Where(o => o.WasReturned).Where(source => source != null))
                        {
                            Info("Blacklisting: " + source.Sender);
                            write.WriteLine(source.Sender);
                        }
                    }
                }
            }
            return false;
        }

        public bool IsRecipientBlacklisted(string recipient)
        {
            if (!File.Exists(BlacklistFileLocation))
            {
                Info("Created Blacklist File");
                File.Create(BlacklistFileLocation);
            }
            else if (File.Exists(BlacklistFileLocation))
            {
                if (File.ReadAllText(BlacklistFileLocation).Contains(recipient))
                Warning("Recipient is blacklisted, please remove '" + recipient + "'");
                return true;
            }
            return false;
        }

        public void AddBagItemsToMailPackage(string itemName)
        {
            Lua.DoString("for b=0,4 do for s=0,32 do local l=GetContainerItemLink(b,s)if l and l:find(\"" + itemName +
                         "\")and select(2,GetContainerItemInfo(b,s))==20 then UseContainerItem(b,s)end end end");
        }

        public void SleepForLagDuration()
        {
            Thread.Sleep(new Random().Next(400, 700));
        }

        public void SetMail(string recipient, string goldAmount, string silverAmount, string copperAmount)
        {
            Lua.DoString(MailFrameTab2Click);
            SleepForLagDuration();
            Lua.DoString(SendMailCodButtonClick);
            Lua.DoString(SetGold +
                         goldAmount +
                         ClosingBracket);
            Lua.DoString(SetSilver +
                         silverAmount +
                         ClosingBracket);
            Lua.DoString(SetCopper +
                         copperAmount +
                         ClosingBracket);
            Thread.Sleep(ShortBreak);
            Lua.DoString(SendMailNameEditBox +
                         recipient +
                         ClosingBracket);
        }

        public string IsMailPackageFull()
        {
            SleepForLagDuration();
            return Lua.GetReturnVal<string>(GetSendMailItem12, Zero);
        }

        public int MailPackageSendCount(string recipient)
        {
            MatchCollection collection = Regex.Matches(File.ReadAllText(MainPath +
                                                                        DoubleDash +
                                                                        Year +
                                                                        DoubleDash +
                                                                        Month +
                                                                        DoubleDash +
                                                                        DayNumber +
                                                                        DoubleDash +
                                                                        DayOfWeek +
                                                                        TxtFileExtension), recipient);
            return collection.Count;
        }

        public void InitializeCod1() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod1ItemToSend);

            SetMail(MIRsettings.Instance.Cod1Recipient,
                    MIRsettings.Instance.Cod1AmountGold,
                    MIRsettings.Instance.Cod1AmountSilver,
                    MIRsettings.Instance.Cod1AmountCopper);

            if (IsMailPackageFull() != null)
            {
                SendingMailLog(MIRsettings.Instance.Cod1ItemToSend,
                               MIRsettings.Instance.Cod1Recipient,
                               MIRsettings.Instance.Cod1AmountGold,
                               MIRsettings.Instance.Cod1AmountSilver,
                               MIRsettings.Instance.Cod1AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod1Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod1Recipient) >= MIRsettings.Instance.TbCod1)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod1Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod1);
            }
        }

        public void InitializeCod2() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod2ItemToSend);

            SetMail(MIRsettings.Instance.Cod2Recipient,
                    MIRsettings.Instance.Cod2AmountGold,
                    MIRsettings.Instance.Cod2AmountSilver,
                    MIRsettings.Instance.Cod2AmountCopper);

            if (IsMailPackageFull() != null)
            {
                SendingMailLog(MIRsettings.Instance.Cod2ItemToSend,
                               MIRsettings.Instance.Cod2Recipient,
                               MIRsettings.Instance.Cod2AmountGold,
                               MIRsettings.Instance.Cod2AmountSilver,
                               MIRsettings.Instance.Cod2AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod2Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod2Recipient) >= MIRsettings.Instance.TbCod2)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod2Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod2);
            }
        }

        public void InitializeCod3() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod3ItemToSend);

            SetMail(MIRsettings.Instance.Cod3Recipient,
                    MIRsettings.Instance.Cod3AmountGold,
                    MIRsettings.Instance.Cod3AmountSilver,
                    MIRsettings.Instance.Cod3AmountCopper);

            if (IsMailPackageFull() != null)
            {
                SendingMailLog(MIRsettings.Instance.Cod3ItemToSend,
                               MIRsettings.Instance.Cod3Recipient,
                               MIRsettings.Instance.Cod3AmountGold,
                               MIRsettings.Instance.Cod3AmountSilver,
                               MIRsettings.Instance.Cod3AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod3Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod3Recipient) >= MIRsettings.Instance.TbCod3)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod3Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod3);
            }
        }

        public void InitializeCod4() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod4ItemToSend);

            SetMail(MIRsettings.Instance.Cod4Recipient,
                    MIRsettings.Instance.Cod4AmountGold,
                    MIRsettings.Instance.Cod4AmountSilver,
                    MIRsettings.Instance.Cod4AmountCopper);

            if (IsMailPackageFull() != null)
            {
                SendingMailLog(MIRsettings.Instance.Cod4ItemToSend,
                               MIRsettings.Instance.Cod4Recipient,
                               MIRsettings.Instance.Cod4AmountGold,
                               MIRsettings.Instance.Cod4AmountSilver,
                               MIRsettings.Instance.Cod4AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod4Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod4Recipient) >= MIRsettings.Instance.TbCod4)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod4Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod4);
            }
        }

        public void InitializeCod5() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod5ItemToSend);

            SetMail(MIRsettings.Instance.Cod5Recipient,
                    MIRsettings.Instance.Cod5AmountGold,
                    MIRsettings.Instance.Cod5AmountSilver,
                    MIRsettings.Instance.Cod5AmountCopper);

            if (IsMailPackageFull() != null)
            {
                SendingMailLog(MIRsettings.Instance.Cod5ItemToSend,
                               MIRsettings.Instance.Cod5Recipient,
                               MIRsettings.Instance.Cod5AmountGold,
                               MIRsettings.Instance.Cod5AmountSilver,
                               MIRsettings.Instance.Cod5AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod5Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod5Recipient) >= MIRsettings.Instance.TbCod5)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod5Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod5);
            }
        }

        public void InitializeCod6() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod6ItemToSend);

            SetMail(MIRsettings.Instance.Cod6Recipient,
                    MIRsettings.Instance.Cod6AmountGold,
                    MIRsettings.Instance.Cod6AmountSilver,
                    MIRsettings.Instance.Cod6AmountCopper);

            if (IsMailPackageFull() != null)
            {
                SendingMailLog(MIRsettings.Instance.Cod6ItemToSend,
                               MIRsettings.Instance.Cod6Recipient,
                               MIRsettings.Instance.Cod6AmountGold,
                               MIRsettings.Instance.Cod6AmountSilver,
                               MIRsettings.Instance.Cod6AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod6Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod6Recipient) >= MIRsettings.Instance.TbCod6)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod6Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod6);
            }
        }

        public void InitializeCod7() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod7ItemToSend);

            SetMail(MIRsettings.Instance.Cod7Recipient,
                    MIRsettings.Instance.Cod7AmountGold,
                    MIRsettings.Instance.Cod7AmountSilver,
                    MIRsettings.Instance.Cod7AmountCopper);

            if (IsMailPackageFull() != null)
            {
                SendingMailLog(MIRsettings.Instance.Cod7ItemToSend,
                               MIRsettings.Instance.Cod7Recipient,
                               MIRsettings.Instance.Cod7AmountGold,
                               MIRsettings.Instance.Cod7AmountSilver,
                               MIRsettings.Instance.Cod7AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod7Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod7Recipient) >= MIRsettings.Instance.TbCod7)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod7Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod7);
            }
        }

        public void InitializeCod8() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod8ItemToSend);

            SetMail(MIRsettings.Instance.Cod8Recipient,
                    MIRsettings.Instance.Cod8AmountGold,
                    MIRsettings.Instance.Cod8AmountSilver,
                    MIRsettings.Instance.Cod8AmountCopper);

            if (IsMailPackageFull() != null &&
                MailPackageSendCount(MIRsettings.Instance.Cod8Recipient) <= MIRsettings.Instance.TbCod8)
            {
                SendingMailLog(MIRsettings.Instance.Cod8ItemToSend,
                               MIRsettings.Instance.Cod8Recipient,
                               MIRsettings.Instance.Cod8AmountGold,
                               MIRsettings.Instance.Cod8AmountSilver,
                               MIRsettings.Instance.Cod8AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod8Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod8Recipient) >= MIRsettings.Instance.TbCod8)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod8Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod8);
            }
        }

        public void InitializeCod9() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod9ItemToSend);

            SetMail(MIRsettings.Instance.Cod9Recipient,
                    MIRsettings.Instance.Cod9AmountGold,
                    MIRsettings.Instance.Cod9AmountSilver,
                    MIRsettings.Instance.Cod9AmountCopper);

            if (IsMailPackageFull() != null &&
                MailPackageSendCount(MIRsettings.Instance.Cod9Recipient) <= MIRsettings.Instance.TbCod9)
            {
                SendingMailLog(MIRsettings.Instance.Cod9ItemToSend,
                               MIRsettings.Instance.Cod9Recipient,
                               MIRsettings.Instance.Cod9AmountGold,
                               MIRsettings.Instance.Cod9AmountSilver,
                               MIRsettings.Instance.Cod9AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod9Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod9Recipient) >= MIRsettings.Instance.TbCod9)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod9Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod9);
            }
        }

        public void InitializeCod10() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod10ItemToSend);

            SetMail(MIRsettings.Instance.Cod10Recipient,
                    MIRsettings.Instance.Cod10AmountGold,
                    MIRsettings.Instance.Cod10AmountSilver,
                    MIRsettings.Instance.Cod10AmountCopper);

            if (IsMailPackageFull() != null &&
                MailPackageSendCount(MIRsettings.Instance.Cod10Recipient) <= MIRsettings.Instance.TbCod10)
            {
                SendingMailLog(MIRsettings.Instance.Cod10ItemToSend,
                               MIRsettings.Instance.Cod10Recipient,
                               MIRsettings.Instance.Cod10AmountGold,
                               MIRsettings.Instance.Cod10AmountSilver,
                               MIRsettings.Instance.Cod10AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod10Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod10Recipient) >= MIRsettings.Instance.TbCod10)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod10Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod10);
            }
        }

        public void InitializeCod11() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod11ItemToSend);

            SetMail(MIRsettings.Instance.Cod11Recipient,
                    MIRsettings.Instance.Cod11AmountGold,
                    MIRsettings.Instance.Cod11AmountSilver,
                    MIRsettings.Instance.Cod11AmountCopper);

            if (IsMailPackageFull() != null &&
                MailPackageSendCount(MIRsettings.Instance.Cod11Recipient) <= MIRsettings.Instance.TbCod11)
            {
                SendingMailLog(MIRsettings.Instance.Cod11ItemToSend,
                               MIRsettings.Instance.Cod11Recipient,
                               MIRsettings.Instance.Cod11AmountGold,
                               MIRsettings.Instance.Cod11AmountSilver,
                               MIRsettings.Instance.Cod11AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod11Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod11Recipient) >= MIRsettings.Instance.TbCod11)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod11Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod11);
            }
        }

        public void InitializeCod12() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod12ItemToSend);

            SetMail(MIRsettings.Instance.Cod12Recipient,
                    MIRsettings.Instance.Cod12AmountGold,
                    MIRsettings.Instance.Cod12AmountSilver,
                    MIRsettings.Instance.Cod12AmountCopper);

            if (IsMailPackageFull() != null &&
                MailPackageSendCount(MIRsettings.Instance.Cod12Recipient) <= MIRsettings.Instance.TbCod12)
            {
                SendingMailLog(MIRsettings.Instance.Cod12ItemToSend,
                               MIRsettings.Instance.Cod12Recipient,
                               MIRsettings.Instance.Cod12AmountGold,
                               MIRsettings.Instance.Cod12AmountSilver,
                               MIRsettings.Instance.Cod12AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod12Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod12Recipient) >= MIRsettings.Instance.TbCod12)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod12Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod12);
            }
        }

        public void InitializeCod13() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod13ItemToSend);

            SetMail(MIRsettings.Instance.Cod13Recipient,
                    MIRsettings.Instance.Cod13AmountGold,
                    MIRsettings.Instance.Cod13AmountSilver,
                    MIRsettings.Instance.Cod13AmountCopper);

            if (IsMailPackageFull() != null &&
                MailPackageSendCount(MIRsettings.Instance.Cod13Recipient) <= MIRsettings.Instance.TbCod13)
            {
                SendingMailLog(MIRsettings.Instance.Cod13ItemToSend,
                               MIRsettings.Instance.Cod13Recipient,
                               MIRsettings.Instance.Cod13AmountGold,
                               MIRsettings.Instance.Cod13AmountSilver,
                               MIRsettings.Instance.Cod13AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod13Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod13Recipient) >= MIRsettings.Instance.TbCod13)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod13Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod13);
            }
        }

        public void InitializeCod14() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod14ItemToSend);

            SetMail(MIRsettings.Instance.Cod14Recipient,
                    MIRsettings.Instance.Cod14AmountGold,
                    MIRsettings.Instance.Cod14AmountSilver,
                    MIRsettings.Instance.Cod14AmountCopper);

            if (IsMailPackageFull() != null &&
                MailPackageSendCount(MIRsettings.Instance.Cod14Recipient) <= MIRsettings.Instance.TbCod14)
            {
                SendingMailLog(MIRsettings.Instance.Cod14ItemToSend,
                               MIRsettings.Instance.Cod14Recipient,
                               MIRsettings.Instance.Cod14AmountGold,
                               MIRsettings.Instance.Cod14AmountSilver,
                               MIRsettings.Instance.Cod14AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod14Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod14Recipient) >= MIRsettings.Instance.TbCod14)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod14Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod14);
            }
        }

        public void InitializeCod15() // checked
        {
            Lua.DoString(ClearSendMail);
            SleepForLagDuration();
            AddBagItemsToMailPackage(MIRsettings.Instance.Cod15ItemToSend);

            SetMail(MIRsettings.Instance.Cod15Recipient,
                    MIRsettings.Instance.Cod15AmountGold,
                    MIRsettings.Instance.Cod15AmountSilver,
                    MIRsettings.Instance.Cod15AmountCopper);

            if (IsMailPackageFull() != null &&
                MailPackageSendCount(MIRsettings.Instance.Cod15Recipient) <= MIRsettings.Instance.TbCod15)
            {
                SendingMailLog(MIRsettings.Instance.Cod15ItemToSend,
                               MIRsettings.Instance.Cod15Recipient,
                               MIRsettings.Instance.Cod15AmountGold,
                               MIRsettings.Instance.Cod15AmountSilver,
                               MIRsettings.Instance.Cod15AmountCopper);

                Thread.Sleep(LongBreak);
                Lua.DoString(ClickSendMailButton);

                WriteRecipientToFile(MIRsettings.Instance.Cod15Recipient);
            }
            else if (IsMailPackageFull() == null)
            {
                Warning("Mail package is not full, not sending mail");
            }
            else if (MailPackageSendCount(MIRsettings.Instance.Cod15Recipient) >= MIRsettings.Instance.TbCod15)
            {
                Warning("Your limit has been reached in sending to " + MIRsettings.Instance.Cod15Recipient +
                        " current cap is at " + MIRsettings.Instance.TbCod15);
            }
        }
    }
}